package com.example.user_module.login;

import android.widget.Toast;

import androidx.room.Room;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SPUtils;
import com.example.base_application_sdk.BaseApplication;
import com.example.common.entity.BaseEntity;
import com.example.common.mvvm.viewmodel.BaseViewModel;
import com.example.user_module.room.AppDatabase;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class LoginViewModel extends BaseViewModel<LoginModel> {

    @Override
    public void onResult(BaseEntity entity) {
        if (entity instanceof RoomUserEntity){
            RoomUserEntity roomUserEntity = (RoomUserEntity) entity;
            SPUtils.getInstance().put("isLogin",true);
            LogUtils.e(roomUserEntity.toString());
            insertRoom(roomUserEntity.data.data);
        }else if (entity instanceof IsLoginEnity){
            Toast.makeText(BaseApplication.getInstance(), "插入成功", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    protected void initData() {

    }

    public void requestUser(String avatar,
                            String description,
                            long expires_time,
                            Integer followCount,
                            Integer likeCount,
                            String name,
                            String qqOpenId,
                            Integer topCommentCount){
        respository.insterUser(avatar,description,expires_time,followCount,
                likeCount,name,qqOpenId,topCommentCount)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }

    public void insertRoom(RoomUserEntity.InsertUserValuesEntity entity){
        AppDatabase build = Room.databaseBuilder(BaseApplication.getInstance(), AppDatabase.class, "login.db").build();
        respository.addRoom(build.userDao(),entity)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }

}
